#!/bin/sh
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

# We might need to call this for YTLB as well for V4
verify_drm_state()
{
  # If the certificate has changed we must remove all drm files
  [ -e /user/drm/drmcert ] && read drmcert </user/drm/drmcert
  # Bruno's sha1sum doesn't have check support
  fscert=$(sha1sum /usr/local/licenses/playready.bin)
  if [ "$drmcert" != "$fscert" ]; then
    echo Certificate doesn\'t match current state, clearing files
    rm -rf /user/drm/*
    echo "$fscert" > /user/drm/drmcert
  fi
}

verify_vudu_platform()
{
  [ -e /tmp/platform ] && read platform </tmp/platform
  if [ -e /user/vudu/vudu-json-settings.txt ] && \
   grep -q GOOGG100 /user/vudu/vudu-json-settings.txt && \
   [ "$platform" = "GFHD200" ]; then
     echo "Clearing out VUDU state due to wrong platform"
     rm -rf /user/vudu/*
  fi
}

read appname opts </tmp/runappold

hdsd_mode=1

# Use same output resolution as miniclient was using
case $VIDEO_RESOLUTION in
  1920x1080p@24*) res=1080p ;;
  1920x1080p@60*) res=1080p ;;
  1920x1080i@60*) res=1080i ;;
  720x480p@60*)   res=480p  ;;
  720x480i@60*)   res=480i; hdsd_mode=0 ;;
  *)              res=720p  ;;
esac
echo "res='$res'"

export hdsd_mode
export DFBARGS="linux-input-ir-only,no-sighandler,mode=1280x720,res=$res"

# Verify if we need to clear any activation data
[ -e /rw/sage/deactivate_netflix ] && /app/client/clear-app netflix
[ -e /rw/sage/deactivate_vudu ] && /app/client/clear-app vudu
[ -e /rw/sage/deactivate_youtube ] && /app/client/clear-app youtube

if [ "$appname" != "waitpower" ]; then
  echo "Starting activity $appname"
  start_time=`date +%s`
fi

case $appname in
  netflix)
    cd /usr/local/bin/netflix
    export PATH=$PWD:$PATH
    export sw_picture_decode=y
    chmod 755 /user/netflix/nrd
    if ! has-secure-video-path; then
      verify_drm_state
    fi
    if verify_playready; then
      echo "Playready state verified."
    else
      echo "Playready state invalid, removing files."
      rm -rf /user/drm
      mkdir /user/drm
    fi

    FRIENDLYNAME="Google Fiber TV Box"
    if [ -e /config/nickname ]; then
      FRIENDLYNAME="$(cat /config/nickname)"
    fi
    netflix --dpi-friendlyname "$FRIENDLYNAME" $opts 2>&1 | logos netflix
    ;;
  vudu)
    cd /usr/local/bin/vudu
    export PATH=$PWD:$PATH
    verify_vudu_platform
    export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH
    export VUDU_LINK_INTERFACE=br0
    vudu $opts 2>&1 | logos vudu
    ;;
  youtube)
    cd /usr/local/bin/webkitGl?
    export PATH=$PWD:$PATH
    [ -z "$opts" ] && opts='https://www.youtube.com/tv'
    # YouTube is run under an alivemonitor. Setting any of the keepalive flags
    # is sufficient to turn it on.
    start_chromium --fiber-keepalive-file $opts 2>&1 | logos youtube
    ;;
  miracast)
    /usr/local/bin/trellis/StandaloneWFDPlayer $opts 2>&1 | logos miracast
    # Once the session is over, kill the miracast listener.
    # This will cause us to stop advertising ourselves as a sink.
    rm -f /tmp/miracast-listener
    ;;
  waitpower)
    echo 1 > /tmp/poweroff
    ;;
  idlesleep)
    /app/client/idlesleep 2>&1 | logos idlesleep
    ;;
  *)
    echo "Unknown application." >&2
    exit 1
    ;;
esac

if [ "$appname" != "waitpower" ]; then
  echo "Exited activity $appname after $(( `date +%s` - $start_time )) seconds"
fi
